package main

import (
	"context"
	"errors"
	"fmt"
	"gitee.com/trackertrader/rpcx/client"
	"gitee.com/trackertrader/rpcx/log"
	"gitee.com/trackertrader/rpcx/protocol"
	"gitee.com/trackertrader/trackersdk/api"
	"github.com/urfave/cli/v2"
	"os"
	"runtime"
)

// 停止Quantrecorder命令
var QuantrecorderStopCmd = &cli.Command{
	Name:  "stop",
	Usage: "停止正在运行的quantrecorder",
	Flags: []cli.Flag{},
	Action: func(cctx *cli.Context) error {
		addr := &Conf.Host
		serviceDiscovery, _ := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
		opt := client.DefaultOption
		opt.SerializeType = protocol.JSON
		xClient := client.NewXClient("QuoteRPC", client.Failtry, client.RandomSelect, serviceDiscovery, opt)
		defer xClient.Close()
		args := api.Args{Action: "quoterecorder stop", Data: "action data"}
		reply := &api.Reply{}
		err := xClient.Call(context.Background(), "StopQuantServer", args, reply)
		if err != nil {
			return err
		}
		if 0 != reply.Code {
			return errors.New(reply.Msg)
		}
		return nil
	},
}

/**
 * @Description: 行情记录启动命令
 */
var QuantrecorderCmd = &cli.Command{
	Name:  "run",
	Usage: "启动行情记录",
	Action: func(cctx *cli.Context) error {
		fmt.Println("OS: " + runtime.GOOS)
		fmt.Println("QUOTERECORDER_HOME: " + os.Getenv("QUOTERECORDER_HOME"))
		fmt.Println("VERSION: " + BuildVersion)

		// 创建行情服务
		QuoteService = newQuoteRecorder()
		if err := QuoteService.Start(cctx); err != nil {
			log.Fatal(err)
			return err
		}
		return serveRPC(cctx)
	},
	Subcommands: []*cli.Command{},
}
